package com.sp.scmdb.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.LabelIOBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Department;
import com.sp.scmdb.entity.Duty;
import com.sp.scmdb.entity.LabelReadingMachine;
import com.sp.scmdb.entity.Map;
import com.sp.scmdb.entity.WorkType;
import com.sp.scmdb.exception.ExcelException;
import com.sp.scmdb.service.DepartmentService;
import com.sp.scmdb.service.DutyService;
import com.sp.scmdb.service.ExportExcelService;
import com.sp.scmdb.service.LabelIOService;
import com.sp.scmdb.service.MapService;
import com.sp.scmdb.service.ReaderService;
import com.sp.scmdb.service.WorkTypeService;

@Controller("labelIOAction")
@Scope("prototype")
public class LabelIOAction extends BaseAction<LabelIOBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Log log = LogFactory.getLog(LabelIOAction.class);
	private LabelIOService labelIOService;
	private DepartmentService departmentService;
	private DutyService dutyService;
	private WorkTypeService workTypeService;
	private ReaderService readerService;
	private MapService mapService;
	private ExportExcelService exportExcelService;
	private InputStream excelStream;
	private String fileName;

	public InputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	@Autowired
	public void setExportExcelService(ExportExcelService exportExcelService) {
		this.exportExcelService = exportExcelService;
	}

	@Autowired
	public void setMapService(MapService mapService) {
		this.mapService = mapService;
	}

	@Autowired
	public void setReaderService(ReaderService readerService) {
		this.readerService = readerService;
	}

	@Autowired
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	@Autowired
	public void setDutyService(DutyService dutyService) {
		this.dutyService = dutyService;
	}

	@Autowired
	public void setWorkTypeService(WorkTypeService workTypeService) {
		this.workTypeService = workTypeService;
	}

	@Autowired
	public void setLabelIOService(LabelIOService labelIOService) {
		this.labelIOService = labelIOService;
	}

	public String initLabelIO() {
		try {
			session.remove("labelIOBean");
			UserBean user = (UserBean) session.get("user");
			List<Department> depList = departmentService.queryAll(user);
			List<Duty> dutyList = dutyService.queryAll();
			List<WorkType> workTypeList = workTypeService.queryAll();
			request.setAttribute("depList", depList);
			request.setAttribute("dutyList", dutyList);
			request.setAttribute("workTypeList", workTypeList);
			request.setAttribute("startTime", new DateTime().toString("yyyy-MM-dd")); 
			return SUCCESS;
		} catch (Exception e) {
			log.error("init labelIO failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	@SuppressWarnings("unchecked")
	public String findAll() {
		try {
			LabelIOBean labelIOBean = null;
			if (StringUtils.isNotEmpty(pageName)) {
				labelIOBean = (LabelIOBean) session.get("labelIOBean");
			}
			UserBean user = (UserBean) session.get("user");
			List<Department> depList = departmentService.queryAll(user);
			List<Duty> dutyList = dutyService.queryAll();
			List<WorkType> workTypeList = workTypeService.queryAll();
			request.setAttribute("depList", depList);
			request.setAttribute("dutyList", dutyList);
			request.setAttribute("workTypeList", workTypeList);
			if (StringUtils.isNotEmpty(entity.getInTime()) && StringUtils.isNotEmpty(entity.getOutTime())) {
				setEntity(labelIOService.queryLabelIOAll(entity, labelIOBean, user));
				session.put("labelIOBean", entity);
			} else {
				session.remove("labelIOBean");
				messageSession(CommonParamter.CHECK_FLG, "请输入查询时间段");
			}
			return SUCCESS;
		}catch (Exception e) {
			log.error("get labelIO failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	@SuppressWarnings("unchecked")
	public String findDetail() {
		try {
			String empID = request.getParameter("id");
			if (StringUtils.isNotEmpty(empID)) {
				entity.setEmpID(empID);
			}
			LabelIOBean labelIOBean = null;
			if (StringUtils.isNotEmpty(pageName)) {
				labelIOBean = (LabelIOBean) session.get("labelIOBeanDetail");
			}
			setEntity(labelIOService.queryLabelIODetail(entity, labelIOBean));
			session.put("labelIOBeanDetail", entity);
			return "showDetail";
		} catch (Exception e) {
			log.error("get labelIO detail failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	@SuppressWarnings("unchecked")
	public String findHisDetail() {
		try {
			String empID = request.getParameter("id");
			if (StringUtils.isNotEmpty(empID)) {
				entity.setEmpID(empID);
			}
			LabelIOBean labelIOBean = null;
			if (StringUtils.isNotEmpty(pageName)) {
				labelIOBean = (LabelIOBean) session.get("labelIOBeanHisDetail");
			}
			setEntity(labelIOService.queryHisLabelIODetail(entity, labelIOBean));
			session.put("labelIOBeanHisDetail", entity);
			return "showHisDetail";
		} catch (Exception e) {
			log.error("get labelIO detail failed! reason:" + e.getMessage());
			return "fail";
		}
	}
	public String drawHisTrack() {
		try {
			List<LabelReadingMachine> listRead = readerService.queryAll();
			request.setAttribute("listRead", listRead);
			List<Map> listMap = mapService.queryIsUsed();
			request.setAttribute("listMap", listMap);
			LabelIOBean labelIOBean = (LabelIOBean) session.get("labelIOBeanHisDetail");
			String points = labelIOService.getTrackPath(labelIOBean);
			request.setAttribute("points", points.substring(0, points.lastIndexOf(";")));
			return "track";
		} catch (Exception e) {
			log.error("get labelIO detail failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String excelHisExport() {
		Workbook wb = new HSSFWorkbook();
		LabelIOBean labelIOBean = (LabelIOBean) session.get("labelIOBeanHisDetail");
		try {
			if (labelIOBean != null) {
				wb = exportExcelService.createLabelIOExcel(labelIOBean);
			}
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			String tableName = new String("轨迹信息报表".getBytes(), "ISO-8859-1");
			this.fileName = tableName + ".xls";
			wb.write(out);
			excelStream = new ByteArrayInputStream(out.toByteArray());
			out.flush();
			out.close();
			return "outExcel";
		} catch (ExcelException excel) {
			log.error("create excel failed! reason:" + excel.getMessage());
			return "fail";
		} catch (IOException e) {
			log.error("export labelIO detail failed! reason:" + e.getMessage());
			return "fail";
		}

	}
	public String drawTrack() {
		try {
			List<LabelReadingMachine> listRead = readerService.queryAll();
			request.setAttribute("listRead", listRead);
			List<Map> listMap = mapService.queryIsUsed();
			request.setAttribute("listMap", listMap);
			LabelIOBean labelIOBean = (LabelIOBean) session.get("labelIOBeanDetail");
			String points = labelIOService.getTrackPath(labelIOBean);
			request.setAttribute("points", points.substring(0, points.lastIndexOf(";")));
			return "track";
		} catch (Exception e) {
			log.error("get labelIO detail failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	public String excelExport() {
		Workbook wb = new HSSFWorkbook();
		LabelIOBean labelIOBean = (LabelIOBean) session.get("labelIOBeanDetail");
		try {
			if (labelIOBean != null) {
				wb = exportExcelService.createLabelIOExcel(labelIOBean);
			}
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			String tableName = new String("轨迹信息报表".getBytes(), "ISO-8859-1");
			this.fileName = tableName + ".xls";
			wb.write(out);
			excelStream = new ByteArrayInputStream(out.toByteArray());
			out.flush();
			out.close();
			return "outExcel";
		} catch (ExcelException excel) {
			log.error("create excel failed! reason:" + excel.getMessage());
			return "fail";
		} catch (IOException e) {
			log.error("export labelIO detail failed! reason:" + e.getMessage());
			return "fail";
		}

	}

}
